﻿//-----------------------------------------------------------------------
// <copyright file="Form1.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Net;
using System.Text;
using System.Windows.Forms;
using GateShift.Client.Libraries.Networking;
using GateShift.Common.Networking.Events;

#endregion

namespace SampleClient
{
    /// <summary>
    /// Demonstration Chat Window
    /// </summary>
    public partial class Form1 : Form
    {
        private readonly ClientSocket _socket;

        /// <summary>
        /// Initializes a new instance of the Form1 class
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            _socket = new ClientSocket { CompressTraffic = true };

            byte[] encryptKey = Convert.FromBase64String(Properties.Settings.Default.EncryptionKey);
            byte[] encryptIV = Convert.FromBase64String(Properties.Settings.Default.EncryptionIV);
            string encryptName = Properties.Settings.Default.EncryptionName;

            _socket.SetEncryption(encryptName, encryptKey, encryptIV);

            _socket.Connected += Socket_Connected;
            _socket.Disconnected += Socket_Disconnected;
            _socket.PacketRecieved += Socket_PacketRecieved;
        }

        /// <summary>
        /// Delegate for adding chat content from a different thread
        /// </summary>
        /// <param name="s">String to add</param>
        private delegate void AddContent(string s);

        /// <summary>
        /// Event handler for recieving a packet
        /// </summary>
        /// <param name="sender">ClientSocket calling the event</param>
        /// <param name="e">DataRecievedEventArgs with information about the event</param>
        private void Socket_PacketRecieved(object sender, DataRecievedEventArgs e)
        {
            if (e.SecondaryCode != nudChannel.Value)
            {
                return;
            }

            AddContent d = (s) => rtfContent.AppendText(s);
            Invoke(d, Encoding.ASCII.GetString(e.Data) + "\n");
        }

        /// <summary>
        /// Event handler for disconnecting
        /// </summary>
        /// <param name="sender">ClientSocket calling the event</param>
        /// <param name="e">Empty EventArgs</param>
        private void Socket_Disconnected(object sender, EventArgs e)
        {
            AddContent d = (s) => rtfContent.AppendText(s);
            Invoke(d, string.Format("Disconnected from {0}\n", txtIP.Text));
        }

        /// <summary>
        /// Event handler for connecting
        /// </summary>
        /// <param name="sender">ClientSocket calling the event</param>
        /// <param name="e">Empty EventArgs</param>
        private void Socket_Connected(object sender, EventArgs e)
        {
            // It's on a different thread, so use Invoke.
            rtfContent.AppendText(string.Format("Connected to {0}\n", txtIP.Text));
        }

        /// <summary>
        /// Event handler for sending a message
        /// </summary>
        /// <param name="sender">Button calling this event</param>
        /// <param name="e">Empty EventArgs</param>
        private void Send_Click(object sender, EventArgs e)
        {
            string sendString = string.Format("{0}) {1}", txtChatName.Text, txtMessage.Text);
            _socket.SendData(1, (byte)nudChannel.Value, Encoding.ASCII.GetBytes(sendString), chkEncrypt.Checked);
        }

        /// <summary>
        /// Event handler for beginning the connection
        /// </summary>
        /// <param name="sender">ToolStripMenuItem calling this event</param>
        /// <param name="e">Empty EventArgs</param>
        private void ConnectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _socket.ConnectionEndPoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), 8888);
            _socket.Connect();
        }

        /// <summary>
        /// Event handler for exiting
        /// </summary>
        /// <param name="sender">ToolStripMenuItem calling this event</param>
        /// <param name="e">Empty EventArgs</param>
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Event handler for the file menu opening
        /// </summary>
        /// <param name="sender">ToolStripMenuItem calling this event</param>
        /// <param name="e">Empty EventArgs</param>
        private void FileToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            connectToolStripMenuItem.Enabled = !_socket.IsConnected;
            disconnectToolStripMenuItem.Enabled = _socket.IsConnected;
        }

        /// <summary>
        /// Event handler for beginning the disconnection
        /// </summary>
        /// <param name="sender">ToolStripMenuItem calling this event</param>
        /// <param name="e">Empty EventArgs</param>
        private void DisconnectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _socket.Disconnect();
        }
    }
}
